package com.example.distillapplication.ui.fragment.log;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.distillapplication.R;
import com.example.distillapplication.data.entity.OperationLog;
import com.example.distillapplication.databinding.FragmentLogListBinding;
import com.example.distillapplication.ui.fragment.base.BaseFragment;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class LogListFragment extends BaseFragment implements IGotoDataFragment {
    FragmentLogListBinding fragmentLogListBinding;
    MyAdapter myAdapter;

    public LogListFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_log_list, container, false);
        fragmentLogListBinding = FragmentLogListBinding.bind(view);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        myAdapter = new MyAdapter();
        myAdapter.setGotoDataFragment(this);
        fragmentLogListBinding.recycleview.setLayoutManager(new LinearLayoutManager(this.getContext()));
        fragmentLogListBinding.recycleview.setAdapter(myAdapter);
        mDataBaseViewModel.allOperationLog.observe(getViewLifecycleOwner(), new Observer<List<OperationLog>>() {
            @Override
            public void onChanged(List<OperationLog> operationLogs) {
                myAdapter.setOperationLogs(operationLogs);
                myAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void goToDataFragment(Bundle bundle) {
        NavController controller = Navigation.findNavController(getView());
        controller.navigate(R.id.action_logListFragment_to_dataFragment, bundle);
    }
}
